package com.cc.service.impl;
import com.cc.entity.SysRight;
import com.cc.entity.SysUser;
import com.cc.entity.SysUserExample;
import com.cc.mapper.SysUserMapper;
import com.cc.service.ISysUserService;
import com.cc.util.PasswordHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 新梦想.陈超
 * @version 2021.2
 * @Description: {TODO}
 * @date 2022/5/25 上午9:13
 */
@Service
@Slf4j
public class SysUserServiceImpl implements ISysUserService {
    @Resource
    private SysUserMapper sysUserMapper;
    @Override
    public SysUser login(SysUser sysUser) {
        //select * from sys_user where usrName= xx or usrPassword=yy
        //将明文加密，加密后再跟数据的密文对比
        SysUser encryptUser = new PasswordHelper().encrypt(sysUser);

        log.info("encryptUser:{}",encryptUser);
//        SysUserExample userExample = new SysUserExample();
//        SysUserExample.Criteria criteria = userExample.createCriteria();
//        criteria.andUsrNameEqualTo(encryptUser.getUsrName()).andUsrPasswordEqualTo(encryptUser.getUsrPassword());
//
//        List<SysUser> lgnUserList = sysUserMapper.selectByExample(userExample);

        SysUser lgnUser = sysUserMapper.queryUserRelationRoleAndRight(encryptUser);
        log.info("用户角色：{}",lgnUser.getSysRole());
        List<SysRight> rightList = lgnUser.getSysRole().getRightList();
        rightList.forEach(right->{
            log.info("用户权利：{}",right.getRightTip());
        });


        return lgnUser;
    }

    @Override
    public SysUser queryUserByPrincipal(String principal) {
        SysUserExample userExample = new SysUserExample();
        SysUserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsrNameEqualTo(principal);
        List<SysUser> sysUserList = sysUserMapper.selectByExample(userExample);
        if(sysUserList==null||sysUserList.size()==0){
            throw new RuntimeException("用户名或密码错误");
        }
        return sysUserList.get(0);
    }
}
